home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / doctempl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  12.1 KB  |  447 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE2_SEG
  14. #pragma code_seg(AFX_CORE2_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CDocTemplate construction/destruction
  26.  
  27. CDocTemplate::CDocTemplate(UINT nIDResource, CRuntimeClass* pDocClass,
  28.     CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass)
  29. {
  30.     ASSERT_VALID_IDR(nIDResource);
  31.     ASSERT(pDocClass == NULL ||
  32.         pDocClass->IsDerivedFrom(RUNTIME_CLASS(CDocument)));
  33.     ASSERT(pFrameClass == NULL ||
  34.         pFrameClass->IsDerivedFrom(RUNTIME_CLASS(CFrameWnd)));
  35.     ASSERT(pViewClass == NULL ||
  36.         pViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));
  37.  
  38.     m_nIDResource = nIDResource;
  39.     m_nIDServerResource = NULL;
  40.     m_nIDEmbeddingResource = NULL;
  41.     m_nIDContainerResource = NULL;
  42.  
  43.     m_pDocClass = pDocClass;
  44.     m_pFrameClass = pFrameClass;
  45.     m_pViewClass = pViewClass;
  46.     m_pOleFrameClass = NULL;
  47.     m_pOleViewClass = NULL;
  48.  
  49. #if !defined(_WIN32_WCE_NO_OLE)
  50.     m_pAttachedFactory = NULL;
  51. #endif // _WIN32_WCE_NO_OLE
  52.     m_hMenuInPlace = NULL;
  53.     m_hAccelInPlace = NULL;
  54.     m_hMenuEmbedding = NULL;
  55.     m_hAccelEmbedding = NULL;
  56.     m_hMenuInPlaceServer = NULL;
  57.     m_hAccelInPlaceServer = NULL;
  58.  
  59.     // add to pStaticList if constructed as static instead of on heap
  60.     if (CDocManager::bStaticInit)
  61.     {
  62.         m_bAutoDelete = FALSE;
  63.         if (CDocManager::pStaticList == NULL)
  64.             CDocManager::pStaticList = new CPtrList;
  65.         if (CDocManager::pStaticDocManager == NULL)
  66.             CDocManager::pStaticDocManager = new CDocManager;
  67.         CDocManager::pStaticList->AddTail(this);
  68.     }
  69.     else
  70.     {
  71.         m_bAutoDelete = TRUE;   // usually allocated on the heap
  72.         LoadTemplate();
  73.     }
  74. }
  75.  
  76. void CDocTemplate::LoadTemplate()
  77. {
  78.     if (m_strDocStrings.IsEmpty() && !m_strDocStrings.LoadString(m_nIDResource))
  79.     {
  80.         TRACE1("Warning: no document names in string for template #%d.\n",
  81.             m_nIDResource);
  82.     }
  83.  
  84.     if (m_nIDEmbeddingResource != 0 && m_hMenuEmbedding == NULL)
  85.     {
  86.         // load menu to be used while editing an embedding (as a server)
  87.         HINSTANCE hInst = AfxFindResourceHandle(
  88.             MAKEINTRESOURCE(m_nIDEmbeddingResource), RT_MENU);
  89.         m_hMenuEmbedding =
  90.             ::LoadMenu(hInst, MAKEINTRESOURCE(m_nIDEmbeddingResource));
  91.         m_hAccelEmbedding =
  92.             ::LoadAccelerators(hInst, MAKEINTRESOURCE(m_nIDEmbeddingResource));
  93.     }
  94.     if (m_nIDServerResource != 0 && m_hMenuInPlaceServer == NULL)
  95.     {
  96.         // load menu to be used while editing in-place (as a server)
  97.         HINSTANCE hInst = AfxFindResourceHandle(
  98.             MAKEINTRESOURCE(m_nIDServerResource), RT_MENU);
  99.         m_hMenuInPlaceServer = ::LoadMenu(hInst,
  100.             MAKEINTRESOURCE(m_nIDServerResource));
  101.         m_hAccelInPlaceServer = ::LoadAccelerators(hInst,
  102.             MAKEINTRESOURCE(m_nIDServerResource));
  103.     }
  104.  
  105.     if (m_nIDContainerResource != 0 && m_hMenuInPlace == NULL)
  106.     {
  107.         // load menu to be used while in-place editing session (as a container)
  108.         HINSTANCE hInst = AfxFindResourceHandle(
  109.             MAKEINTRESOURCE(m_nIDContainerResource), RT_MENU);
  110.         m_hMenuInPlace = ::LoadMenu(hInst,
  111.             MAKEINTRESOURCE(m_nIDContainerResource));
  112.         m_hAccelInPlace = ::LoadAccelerators(hInst,
  113.             MAKEINTRESOURCE(m_nIDContainerResource));
  114.     }
  115. }
  116.  
  117. #if !defined(_WIN32_WCE_NO_OLE)
  118. void CDocTemplate::SetServerInfo(UINT nIDOleEmbedding, UINT nIDOleInPlaceServer,
  119.     CRuntimeClass* pOleFrameClass, CRuntimeClass* pOleViewClass)
  120. {
  121.     ASSERT_VALID_IDR(nIDOleEmbedding);
  122.     if (nIDOleInPlaceServer != 0)
  123.         ASSERT_VALID_IDR(nIDOleInPlaceServer);
  124.     ASSERT(pOleFrameClass == NULL ||
  125.         pOleFrameClass->IsDerivedFrom(RUNTIME_CLASS(CFrameWnd)));
  126.     ASSERT(pOleViewClass == NULL ||
  127.         pOleViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));
  128.  
  129.     m_pOleFrameClass = pOleFrameClass;
  130.     m_pOleViewClass = pOleViewClass;
  131.  
  132.     m_nIDEmbeddingResource = nIDOleEmbedding;
  133.     m_nIDServerResource = nIDOleInPlaceServer;
  134.     if (!CDocManager::bStaticInit)
  135.         LoadTemplate();
  136. }
  137.  
  138. void CDocTemplate::SetContainerInfo(UINT nIDOleInPlaceContainer)
  139. {
  140.     ASSERT(nIDOleInPlaceContainer != 0);
  141.  
  142.     m_nIDContainerResource = nIDOleInPlaceContainer;
  143.     if (!CDocManager::bStaticInit)
  144.         LoadTemplate();
  145. }
  146. #endif // _WIN32_WCE_NO_OLE
  147.  
  148. CDocTemplate::~CDocTemplate()
  149. {
  150.     // delete OLE resources
  151.     if (m_hMenuInPlace != NULL)
  152.         ::DestroyMenu(m_hMenuInPlace);
  153.     if (m_hAccelInPlace != NULL)
  154.         ::FreeResource(m_hAccelInPlace);
  155.     if (m_hMenuEmbedding != NULL)
  156.         ::DestroyMenu(m_hMenuEmbedding);
  157.     if (m_hAccelEmbedding != NULL)
  158.         ::FreeResource(m_hAccelEmbedding);
  159.     if (m_hMenuInPlaceServer != NULL)
  160.         ::DestroyMenu(m_hMenuInPlaceServer);
  161.     if (m_hAccelInPlaceServer != NULL)
  162.         ::FreeResource(m_hAccelInPlaceServer);
  163. }
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CDocTemplate attributes
  167.  
  168. BOOL CDocTemplate::GetDocString(CString& rString, enum DocStringIndex i) const
  169. {
  170.     return AfxExtractSubString(rString, m_strDocStrings, (int)i);
  171. }
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174. // Document management
  175.  
  176. void CDocTemplate::AddDocument(CDocument* pDoc)
  177. {
  178.     ASSERT_VALID(pDoc);
  179.     ASSERT(pDoc->m_pDocTemplate == NULL);   // no template attached yet
  180.     pDoc->m_pDocTemplate = this;
  181. }
  182.  
  183. void CDocTemplate::RemoveDocument(CDocument* pDoc)
  184. {
  185.     ASSERT_VALID(pDoc);
  186.     ASSERT(pDoc->m_pDocTemplate == this);   // must be attached to us
  187.     pDoc->m_pDocTemplate = NULL;
  188. }
  189.  
  190. CDocTemplate::Confidence CDocTemplate::MatchDocType(LPCTSTR lpszPathName,
  191.     CDocument*& rpDocMatch)
  192. {
  193.     ASSERT(lpszPathName != NULL);
  194.     rpDocMatch = NULL;
  195.  
  196.     // go through all documents
  197.     POSITION pos = GetFirstDocPosition();
  198.     while (pos != NULL)
  199.     {
  200.         CDocument* pDoc = GetNextDoc(pos);
  201.         if (AfxComparePath(pDoc->GetPathName(), lpszPathName))
  202.         {
  203.             // already open
  204.             rpDocMatch = pDoc;
  205.             return yesAlreadyOpen;
  206.         }
  207.     }
  208.  
  209.     // see if it matches our default suffix
  210.     CString strFilterExt;
  211.     if (GetDocString(strFilterExt, CDocTemplate::filterExt) &&
  212.       !strFilterExt.IsEmpty())
  213.     {
  214.         // see if extension matches
  215.         ASSERT(strFilterExt[0] == '.');
  216.         LPCTSTR lpszDot = _tcsrchr(lpszPathName, '.');
  217.         if (lpszDot != NULL && lstrcmpi(lpszDot, strFilterExt) == 0)
  218.             return yesAttemptNative; // extension matches, looks like ours
  219.     }
  220.  
  221.     // otherwise we will guess it may work
  222.     return yesAttemptForeign;
  223. }
  224.  
  225. CDocument* CDocTemplate::CreateNewDocument()
  226. {
  227.     // default implementation constructs one from CRuntimeClass
  228.     if (m_pDocClass == NULL)
  229.     {
  230.         TRACE0("Error: you must override CDocTemplate::CreateNewDocument.\n");
  231.         ASSERT(FALSE);
  232.         return NULL;
  233.     }
  234.     CDocument* pDocument = (CDocument*)m_pDocClass->CreateObject();
  235.     if (pDocument == NULL)
  236.     {
  237.         TRACE1("Warning: Dynamic create of document type %hs failed.\n",
  238.             m_pDocClass->m_lpszClassName);
  239.         return NULL;
  240.     }
  241.     ASSERT_KINDOF(CDocument, pDocument);
  242.     AddDocument(pDocument);
  243.     return pDocument;
  244. }
  245.  
  246. /////////////////////////////////////////////////////////////////////////////
  247. // Default frame creation
  248.  
  249. CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther)
  250. {
  251.     if (pDoc != NULL)
  252.         ASSERT_VALID(pDoc);
  253.     // create a frame wired to the specified document
  254.  
  255.     ASSERT(m_nIDResource != 0); // must have a resource ID to load from
  256.     CCreateContext context;
  257.     context.m_pCurrentFrame = pOther;
  258.     context.m_pCurrentDoc = pDoc;
  259.     context.m_pNewViewClass = m_pViewClass;
  260.     context.m_pNewDocTemplate = this;
  261.  
  262.     if (m_pFrameClass == NULL)
  263.     {
  264.         TRACE0("Error: you must override CDocTemplate::CreateNewFrame.\n");
  265.         ASSERT(FALSE);
  266.         return NULL;
  267.     }
  268.     CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject();
  269.     if (pFrame == NULL)
  270.     {
  271.         TRACE1("Warning: Dynamic create of frame %hs failed.\n",
  272.             m_pFrameClass->m_lpszClassName);
  273.         return NULL;
  274.     }
  275.     ASSERT_KINDOF(CFrameWnd, pFrame);
  276.  
  277.     if (context.m_pNewViewClass == NULL)
  278.         TRACE0("Warning: creating frame with no default view.\n");
  279.  
  280.     // create new from resource
  281.     if (!pFrame->LoadFrame(m_nIDResource,
  282.             WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,   // default frame styles
  283.             NULL, &context))
  284.     {
  285.         TRACE0("Warning: CDocTemplate couldn't create a frame.\n");
  286.         // frame will be deleted in PostNcDestroy cleanup
  287.         return NULL;
  288.     }
  289.  
  290.     // it worked !
  291.     return pFrame;
  292. }
  293.  
  294. #if !defined(_WIN32_WCE_NO_OLE)
  295. CFrameWnd* CDocTemplate::CreateOleFrame(CWnd* pParentWnd, CDocument* pDoc,
  296.     BOOL bCreateView)
  297. {
  298.     CCreateContext context;
  299.     context.m_pCurrentFrame = NULL;
  300.     context.m_pCurrentDoc = pDoc;
  301.     context.m_pNewViewClass = bCreateView ? m_pOleViewClass : NULL;
  302.     context.m_pNewDocTemplate = this;
  303.  
  304.     if (m_pOleFrameClass == NULL)
  305.     {
  306.         TRACE0("Warning: pOleFrameClass not specified for doc template.\n");
  307.         return NULL;
  308.     }
  309.  
  310.     ASSERT(m_nIDServerResource != 0); // must have a resource ID to load from
  311.     CFrameWnd* pFrame = (CFrameWnd*)m_pOleFrameClass->CreateObject();
  312.     if (pFrame == NULL)
  313.     {
  314.         TRACE1("Warning: Dynamic create of frame %hs failed.\n",
  315.             m_pOleFrameClass->m_lpszClassName);
  316.         return NULL;
  317.     }
  318.  
  319.     // create new from resource (OLE frames are created as child windows)
  320.     if (!pFrame->LoadFrame(m_nIDServerResource,
  321.         WS_CHILD|WS_CLIPSIBLINGS, pParentWnd, &context))
  322.     {
  323.         TRACE0("Warning: CDocTemplate couldn't create an OLE frame.\n");
  324.         // frame will be deleted in PostNcDestroy cleanup
  325.         return NULL;
  326.     }
  327.  
  328.     // it worked !
  329.     return pFrame;
  330. }
  331. #endif // _WIN32_WCE_NO_OLE
  332.  
  333. void CDocTemplate::InitialUpdateFrame(CFrameWnd* pFrame, CDocument* pDoc,
  334.     BOOL bMakeVisible)
  335. {
  336.     // just delagate to implementation in CFrameWnd
  337.     pFrame->InitialUpdateFrame(pDoc, bMakeVisible);
  338. }
  339.  
  340. /////////////////////////////////////////////////////////////////////////////
  341. // CDocTemplate commands and command helpers
  342.  
  343. BOOL CDocTemplate::SaveAllModified()
  344. {
  345.     POSITION pos = GetFirstDocPosition();
  346.     while (pos != NULL)
  347.     {
  348.         CDocument* pDoc = GetNextDoc(pos);
  349.         if (!pDoc->SaveModified())
  350.             return FALSE;
  351.     }
  352.     return TRUE;
  353. }
  354.  
  355.  
  356. void CDocTemplate::CloseAllDocuments(BOOL)
  357. {
  358.     POSITION pos = GetFirstDocPosition();
  359.     while (pos != NULL)
  360.     {
  361.         CDocument* pDoc = GetNextDoc(pos);
  362.         pDoc->OnCloseDocument();
  363.     }
  364. }
  365.  
  366. void CDocTemplate::OnIdle()
  367. {
  368.     POSITION pos = GetFirstDocPosition();
  369.     while (pos != NULL)
  370.     {
  371.         CDocument* pDoc = GetNextDoc(pos);
  372.         ASSERT_VALID(pDoc);
  373.         ASSERT_KINDOF(CDocument, pDoc);
  374.         pDoc->OnIdle();
  375.     }
  376. }
  377.  
  378. BOOL CDocTemplate::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  379.     AFX_CMDHANDLERINFO* pHandlerInfo)
  380. {
  381.     BOOL bReturn;
  382. #if defined(_WIN32_WCE_NO_OLE)
  383.     bReturn = FALSE;
  384. #else // _WIN32_WCE_NO_OLE
  385.     CCmdTarget* pFactory = DYNAMIC_DOWNCAST(CCmdTarget, m_pAttachedFactory);
  386.  
  387.     if (nCode == CN_OLE_UNREGISTER && pFactory != NULL)
  388.         bReturn = pFactory->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  389.     else
  390.         bReturn = CCmdTarget::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  391. #endif // _WIN32_WCE_NO_OLE
  392.  
  393.     return bReturn;
  394. }
  395.  
  396. /////////////////////////////////////////////////////////////////////////////
  397. // CDocTemplate diagnostics
  398.  
  399. #ifdef _DEBUG
  400. void CDocTemplate::Dump(CDumpContext& dc) const
  401. {
  402.     CCmdTarget::Dump(dc);
  403.  
  404.     dc << "m_nIDResource = " << m_nIDResource;
  405.     dc << "\nm_strDocStrings: " << m_strDocStrings;
  406.  
  407.     if (m_pDocClass)
  408.         dc << "\nm_pDocClass = " << m_pDocClass->m_lpszClassName;
  409.     else
  410.         dc << "\nm_pDocClass = NULL";
  411.  
  412.     if (dc.GetDepth() > 0)
  413.     {
  414.         dc << "\ndocument list = {";
  415.         POSITION pos = GetFirstDocPosition();
  416.         while (pos != NULL)
  417.         {
  418.             CDocument* pDoc = GetNextDoc(pos);
  419.             dc << "\ndocument " << pDoc;
  420.         }
  421.         dc << "\n}";
  422.     }
  423.  
  424.     dc << "\n";
  425. }
  426.  
  427. void CDocTemplate::AssertValid() const
  428. {
  429.     CCmdTarget::AssertValid();
  430.  
  431.     POSITION pos = GetFirstDocPosition();
  432.     while (pos != NULL)
  433.     {
  434.         CDocument* pDoc = GetNextDoc(pos);
  435.         ASSERT_VALID(pDoc);
  436.     }
  437. }
  438. #endif //_DEBUG
  439.  
  440. #ifdef AFX_INIT_SEG
  441. #pragma code_seg(AFX_INIT_SEG)
  442. #endif
  443.  
  444. IMPLEMENT_DYNAMIC(CDocTemplate, CCmdTarget)
  445.  
  446. /////////////////////////////////////////////////////////////////////////////
  447.